﻿using Analysis.Models.EchartModel;
using Bll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utils;

namespace Analysis.Controllers
{
    public class ScanController : Controller
    {
        #region 指标详情
        /// <summary>
        /// 浏览量详情
        /// </summary>
        /// <returns></returns>
        public ActionResult ScanDetail()
        {
            return View();
        }

        /// <summary>
        /// 访客量详情
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginDetail()
        {
            return View();
        }

        /// <summary>
        /// 支付订单量详情
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderNumDetail()
        {
            return View();
        }

        /// <summary>
        /// 客单价详情
        /// </summary>
        /// <returns></returns>
        public ActionResult CostPerOrder()
        {
            return View();
        }

        /// <summary>
        /// 销售额详情
        /// </summary>
        /// <returns></returns>
        public ActionResult SaleDetail()
        {
            return View();
        }
        #endregion

        #region 获取图表
        /// <summary>
        /// 获取浏览量图表数据
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="productName"></param>
        /// <returns></returns>
        public JsonResult GetScanChart(string beginTime, string endTime, string productName)
        {
            Result<BarOrLineModel> result = new Result<BarOrLineModel>();
            List<string> legendList = new List<string>();
            List<string> nameList = new List<string>();
            List<List<decimal>> dataList = new List<List<decimal>>();
            string msg = "";
            int i = 1;
            if (beginTime.Equals(endTime))
            {
                i = 2;
            }

            beginTime = Extend.ConToBeginTime(beginTime);
            endTime = Extend.ConToEndTime(endTime);

            if (i == 1)
            {
                msg = "1";
                legendList.Add("浏览量");
                legendList.Add("增长率");
            }
            else
            {
                msg = "2";
                legendList.Add("当日浏览量");
                legendList.Add("前一日浏览量");
            }
            dataList = GetChartData(1, beginTime, endTime, i,ref nameList, productName);

            BarOrLineModel bar = new BarOrLineModel();
            bar.NameList = nameList;
            bar.LegendList = legendList;
            bar.DataList = dataList;

            result.Status = 200;
            result.Body = bar;
            result.Msg = msg;

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取访客数图表数据
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public JsonResult GetLoginChart(string beginTime, string endTime)
        {
            Result<BarOrLineModel> result = new Result<BarOrLineModel>();
            List<string> legendList = new List<string>();
            List<string> nameList = new List<string>();
            List<List<decimal>> dataList = new List<List<decimal>>();
            string msg = "";
            int i = 1;
            if (beginTime.Equals(endTime))
            {
                i = 2;
            }

            beginTime = Extend.ConToBeginTime(beginTime);
            endTime = Extend.ConToEndTime(endTime);


            if (i == 1)
            {
                msg = "1";
                legendList.Add("访客量");
                legendList.Add("增长率");
            }
            else
            {
                msg = "2";
                legendList.Add("当日访客量");
                legendList.Add("前一日访客量");
            }

            dataList = GetChartData(2, beginTime, endTime, i, ref nameList);

            BarOrLineModel bar = new BarOrLineModel();
            bar.NameList = nameList;
            bar.LegendList = legendList;
            bar.DataList = dataList;

            result.Status = 200;
            result.Body = bar;
            result.Msg = msg;

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取订单量图表数据
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public JsonResult GetOrderChart(string beginTime, string endTime,string plateformType)
        {
            plateformType = string.IsNullOrEmpty(plateformType) ? "-1" : plateformType; //-1：全部 0：实体店 1：电商

            Result<BarOrLineModel> result = new Result<BarOrLineModel>();
            List<string> legendList = new List<string>();
            List<string> nameList = new List<string>();
            List<List<decimal>> dataList = new List<List<decimal>>();
            string msg = "";
            int i = 1;
            if (beginTime.Equals(endTime))
            {
                i = 2;
            }

            beginTime = Extend.ConToBeginTime(beginTime);
            endTime = Extend.ConToEndTime(endTime);


            if (i == 1)
            {
                msg = "1";
                legendList.Add("订单量");
                legendList.Add("增长率");
            }
            else
            {
                msg = "2";
                legendList.Add("当日订单量");
                legendList.Add("前一日订单量");
            }

            dataList = GetChartData(3, beginTime, endTime, i, ref nameList,null,plateformType);

            BarOrLineModel bar = new BarOrLineModel();
            bar.NameList = nameList;
            bar.LegendList = legendList;
            bar.DataList = dataList;

            result.Status = 200;
            result.Body = bar;
            result.Msg = msg;

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取客单价
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="plateformType"></param>
        /// <returns></returns>
        public JsonResult GetCostChart(string beginTime, string endTime, string plateformType)
        {
            plateformType = string.IsNullOrEmpty(plateformType) ? "-1" : plateformType; //-1：全部 0：实体店 1：电商

            Result<BarOrLineModel> result = new Result<BarOrLineModel>();
            List<string> legendList = new List<string>();
            List<string> nameList = new List<string>();
            List<List<decimal>> dataList = new List<List<decimal>>();
            string msg = "";
            int i = 1;
            if (beginTime.Equals(endTime))
            {
                i = 2;
            }

            beginTime = Extend.ConToBeginTime(beginTime);
            endTime = Extend.ConToEndTime(endTime);


            if (i == 1)
            {
                msg = "1";
                legendList.Add("客单价");
                legendList.Add("增长率");
            }
            else
            {
                msg = "2";
                legendList.Add("当日客单价");
                legendList.Add("前一日客单价");
            }

            dataList = GetChartData(4, beginTime, endTime, i, ref nameList, null, plateformType);

            BarOrLineModel bar = new BarOrLineModel();
            bar.NameList = nameList;
            bar.LegendList = legendList;
            bar.DataList = dataList;

            msg += "||" + Session["costPerOrder"];

            result.Status = 200;
            result.Body = bar;
            result.Msg = msg;

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取销售额
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="plateformType"></param>
        /// <returns></returns>
        public JsonResult GetSaleChart(string beginTime, string endTime, string plateformType)
        {
            plateformType = string.IsNullOrEmpty(plateformType) ? "-1" : plateformType; //-1：全部 0：实体店 1：电商

            Result<BarOrLineModel> result = new Result<BarOrLineModel>();
            List<string> legendList = new List<string>();
            List<string> nameList = new List<string>();
            List<List<decimal>> dataList = new List<List<decimal>>();
            string msg = "";
            int i = 1;
            if (beginTime.Equals(endTime))
            {
                i = 2;
            }

            beginTime = Extend.ConToBeginTime(beginTime);
            endTime = Extend.ConToEndTime(endTime);


            if (i == 1)
            {
                msg = "1";
                legendList.Add("销售额");
                legendList.Add("增长率");
            }
            else
            {
                msg = "2";
                legendList.Add("当日销售额");
                legendList.Add("前一日销售额");
            }

            dataList = GetChartData(5, beginTime, endTime, i, ref nameList, null, plateformType);

            BarOrLineModel bar = new BarOrLineModel();
            bar.NameList = nameList;
            bar.LegendList = legendList;
            bar.DataList = dataList;

            result.Status = 200;
            result.Body = bar;
            result.Msg = msg;

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 私有方法

        #region 浏览量
        /// <summary>
        /// 浏览趋势（按天）
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="productName"></param>
        /// <returns></returns>
        private List<TendencyModel> DayScanTendency(string beginTime, string endTime, string productName = null)
        {
            string sql = "";
            if (string.IsNullOrEmpty(productName))
            {
                sql = "select convert(char(10), createTime, 120) as timeStr, count(1) as amount from BrowseRecord where createTime>='" + beginTime + "' and createTime<='" + endTime + "'  group by convert(char(10), createTime, 120)";
            }
            else
            {
                sql = "select convert(char(10), br.createTime, 120) as timeStr, count(1) as amount from BrowseRecord as br,product as p  where br.productId=p.id and br.createTime>='" + beginTime + "' and br.createTime<='" + endTime + "' and p.productName like '%" + productName + "%' group by convert(char(10), br.createTime, 120)";
            }

            List<SelectForTendency> list = BllBBCCommon.SelectEntityBySql<SelectForTendency>("SelectForDayScanTendency", sql);

            List<TendencyModel> result = DayTendency(beginTime, endTime, list);

            return result;
        }

        /// <summary>
        /// 浏览趋势（按小时）
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="productName"></param>
        /// <returns></returns>
        private List<TendencyModel> TimeScanTendency(string beginTime, string endTime, string productName = null)
        {
            string sql = "";
            if (string.IsNullOrEmpty(productName))
            {
                sql = "select convert(char(2), createTime, 108) as timeStr, count(1) as amount from BrowseRecord where createTime>='" + beginTime + "' and createTime<='" + endTime + "'  group by convert(char(2), createTime, 108)";
            }
            else
            {
                sql = "select convert(char(2), br.createTime, 108) as timeStr, count(1) as amount from BrowseRecord as br,product as p  where br.productId=p.id and br.createTime>='" + beginTime + "' and br.createTime<='" + endTime + "' and p.productName like '%" + productName + "%' group by convert(char(2), br.createTime, 108)";
            }

            List<SelectForTendency> list = BllBBCCommon.SelectEntityBySql<SelectForTendency>("SelectForDayScanTendency", sql);

            List<TendencyModel> result = TimeTendency(list);

            return result;
        }
        #endregion

        #region 访客数
        /// <summary>
        /// 访客趋势（按天）
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TendencyModel> DayLoginTendency(string beginTime, string endTime)
        {
            string sql = "select convert(char(10), createTime, 120) as timeStr, count(distinct(userName)) as amount from UserLoginRecord where createTime>='" + beginTime + "' and createTime<='" + endTime + "'  group by convert(char(10), createTime, 120)";

            List<SelectForTendency> list = BllBBCCommon.SelectEntityBySql<SelectForTendency>("UserLoginRecord", sql);

            List<TendencyModel> result= DayTendency( beginTime, endTime,list);

            return result;
        }

        /// <summary>
        /// 访客趋势（按小时）
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TendencyModel> TimeLoginTendency(string beginTime, string endTime)
        {
            string sql_ = "with #UserLoginRecordDistinct as (select min(id) as id, userName from UserLoginRecord where createTime>='" + beginTime + "' and createTime<='" + endTime + "' group by userName)";
            string sql = sql_ + "select convert(char(2), a.createTime, 108) as timeStr, count(1) as amount from UserLoginRecord as a,#UserLoginRecordDistinct as d where a.id=d.id group by convert(char(2), a.createTime, 108)";

            List<SelectForTendency> list = BllBBCCommon.SelectEntityBySql<SelectForTendency>("SelectForDayScanTendency", sql);

            List<TendencyModel> result= TimeTendency(list);

            return result;
        }
        #endregion

        #region 订单量

        /// <summary>
        /// 订单趋势（按天）
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TendencyModel> DayOrderTendency(string beginTime, string endTime, string plateformType)
        {
            string syj_sql = "select convert(char(10), ct, 120) as timeStr, count(1) as amount from t_pos_syd where  ct>='" + beginTime + "' and ct<='" + endTime + "' and yss#=0 and s# not in(select yss# from t_pos_syd where yss#>0 and ct>='"+beginTime+"' and ct<='"+endTime+"')  group by convert(char(10), ct, 120)";
            string bbc_sql = "select convert(char(10), payTime, 120) as timeStr, count(1) as amount from Orders where payStatusId=2 and plateform=3 and  payTime>='" + beginTime + "' and payTime<='" + endTime + "'  group by convert(char(10), payTime, 120)";

            List<SelectForTendency> list = GetOrderNumTendency(plateformType, bbc_sql, syj_sql);

            List<TendencyModel> result = DayTendency(beginTime, endTime, list);

            return result;
        }

        /// <summary>
        /// 订单趋势（按小时）
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TendencyModel> TimeOrderTendency(string beginTime, string endTime,string plateformType)
        {
            string syj_sql = "select convert(char(2), ct, 108) as timeStr, count(1) as amount from t_pos_syd where  ct>='" + beginTime + "' and ct<='" + endTime + "' and yss#=0 and s# not in(select yss# from t_pos_syd where yss#>0 and ct>='" + beginTime + "' and ct<='" + endTime + "')  group by convert(char(2), ct, 108)";
            string bbc_sql = "select convert(char(2), a.payTime, 108) as timeStr, count(1) as amount from orders as a where a.payStatusId=2 and a.plateform=3 and a.payTime>='" + beginTime + "' and a.payTime<='" + endTime + "' group by convert(char(2), a.payTime, 108)";

            List<SelectForTendency> list = GetOrderNumTendency(plateformType, bbc_sql, syj_sql);

            List<TendencyModel> result = TimeTendency(list);

            return result;
        }

        private List<SelectForTendency> GetOrderNumTendency(string plateformType, string bbc_sql, string syj_sql)
        {
            List<SelectForTendency> list = new List<SelectForTendency>();
            if (plateformType == "-1")
            {
                List<SelectForTendency> listBBC = BllBBCCommon.SelectEntityBySql<SelectForTendency>("Orders", bbc_sql);
                List<SelectForTendency> listSYJ = BllSYJCommon.SelectEntityBySql<SelectForTendency>("t_pos_syd", syj_sql);
                list.AddRange(listBBC);

                foreach (SelectForTendency temp in listSYJ)
                {
                    bool flag = list.Select(x => x.timeStr).Contains(temp.timeStr);
                    if (flag)
                    {
                        SelectForTendency xx = list.Where(x => x.timeStr == temp.timeStr).First();
                        xx.amount += temp.amount;
                    }
                    else
                    {
                        list.Add(temp);
                    }
                }
            }
            else if (plateformType == "0")
            {
                //实体店
                List<SelectForTendency> listSYJ = BllSYJCommon.SelectEntityBySql<SelectForTendency>("t_pos_syd", syj_sql);
                list.AddRange(listSYJ);
            }
            else if (plateformType == "1")
            {
                //电商
                List<SelectForTendency> listBBC = BllBBCCommon.SelectEntityBySql<SelectForTendency>("Orders", bbc_sql);
                list.AddRange(listBBC);
            }

            return list;
        }

        #endregion

        #region 单均价

        /// <summary>
        /// 单均价趋势（按天）
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TendencyDecimalModel> DayCostTendency(string beginTime, string endTime, string plateformType)
        {
            string syj_day_income_sql = "select convert(char(10), ct, 120) as timeStr, isnull(sum(dcpayment-dcchange-isnull(mlje,0)),0) as amount from t_pos_syd where ct>='" + beginTime + "' and ct<='" + endTime + "' group by convert(char(10), ct, 120)";
            string bbc_day_income_sql = "select convert(char(10),payTime, 120) as timeStr, isnull(sum(realTotal),0) as amount from orders where paystatusId=2 and payTime<='" + beginTime + "' and payTime>='" + endTime + "' and plateform=3 group by convert(char(10),payTime, 120)";

            string syj_day_order_num_sql = "select convert(char(10), ct, 120) as timeStr,count(1) as amount from t_pos_syd where  ct>='" + beginTime + "' and ct<='" + endTime + "' and yss#=0 and s# not in(select yss# from t_pos_syd where yss#>0 and ct>='" + beginTime + "' and ct<='" + endTime + "')  group by convert(char(10), ct, 120)";
            string bbc_day_order_num_sql = "select convert(char(10), payTime, 120) as timeStr,count(1) as amount from orders where paystatusId=2 and payTime<='" + beginTime + "' and payTime>='" + endTime + "' and plateform=3 group by convert(char(10),payTime, 120)";

            List<SelectForDecimalTendency> list = GetCostPerOrderTendency(plateformType, syj_day_income_sql, bbc_day_income_sql, syj_day_order_num_sql, bbc_day_order_num_sql);

            List<TendencyDecimalModel> result = DayDecimalTendency(beginTime, endTime, list);

            return result;
        }

        /// <summary>
        /// 单均价趋势（按小时）
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TendencyDecimalModel> TimeCostTendency(string beginTime, string endTime, string plateformType)
        {
            string syj_time_income_sql = "select convert(char(2), ct, 108) as timeStr, isnull(sum(dcpayment-dcchange-isnull(mlje,0)),0) as amount from t_pos_syd where ct>='" + beginTime + "' and ct<='" + endTime + "' group by convert(char(2), ct, 108)";
            string bbc_time_income_sql = "select convert(char(2),payTime, 108) as timeStr, isnull(sum(realTotal),0) as amount from orders where paystatusId=2 and payTime<='" + beginTime + "' and payTime>='" + endTime + "' and plateform=3 group by convert(char(2),payTime, 108)";

            string syj_time_order_num_sql = "select convert(char(2), ct, 108) as timeStr,count(1) as amount from t_pos_syd where  ct>='" + beginTime + "' and ct<='" + endTime + "' and yss#=0 and s# not in(select yss# from t_pos_syd where yss#>0 and ct>='" + beginTime + "' and ct<='" + endTime + "')  group by convert(char(2), ct, 108)";
            string bbc_time_order_num_sql = "select convert(char(2),payTime, 108) as timeStr,count(1) as amount from orders where paystatusId=2 and payTime<='" + beginTime + "' and payTime>='" + endTime + "' and plateform=3 group by convert(char(2),payTime, 108)";

            List<SelectForDecimalTendency> list = GetCostPerOrderTendency(plateformType, syj_time_income_sql, bbc_time_income_sql, syj_time_order_num_sql, bbc_time_order_num_sql);

            List<TendencyDecimalModel> result = TimeDecimalTendency(list);

            return result;
        }

        private List<SelectForDecimalTendency> GetCostPerOrderTendency(string plateformType, string syj_day_income_sql, string bbc_day_income_sql, string syj_day_order_num_sql, string bbc_day_order_num_sql)
        {
            List<SelectForDecimalTendency> list = new List<SelectForDecimalTendency>();

            List<SelectForDecimalTendency> allIncomelist = new List<SelectForDecimalTendency>();
            List<SelectForTendency> allNumlist = new List<SelectForTendency>();

            if (plateformType == "-1")
            {

                //全部
             List<SelectForDecimalTendency> syjIncomeList=   BllSYJCommon.SelectEntityBySql<SelectForDecimalTendency>("t_pos_syd", syj_day_income_sql);
             List<SelectForDecimalTendency> bbcIncomeList = BllBBCCommon.SelectEntityBySql<SelectForDecimalTendency>("orders", bbc_day_income_sql);

             List<SelectForTendency> syjNumList = BllSYJCommon.SelectEntityBySql<SelectForTendency>("t_pos_syd", syj_day_order_num_sql);
             List<SelectForTendency> bbcNumList = BllBBCCommon.SelectEntityBySql<SelectForTendency>("orders", bbc_day_order_num_sql);

             allIncomelist.AddRange(syjIncomeList);
             foreach (SelectForDecimalTendency temp in bbcIncomeList)
             {
                 bool flag = allIncomelist.Select(x => x.timeStr).Contains(temp.timeStr);
                 if (flag)
                 {
                     SelectForDecimalTendency xx = allIncomelist.Where(x => x.timeStr == temp.timeStr).First();
                     xx.amount += temp.amount;
                 }
                 else
                 {
                     allIncomelist.Add(temp);
                 }
             }

             allNumlist.AddRange(syjNumList);
             foreach (SelectForTendency temp in bbcNumList)
             {
                 bool flag = allNumlist.Select(x => x.timeStr).Contains(temp.timeStr);
                 if (flag)
                 {
                     SelectForTendency xx = allNumlist.Where(x => x.timeStr == temp.timeStr).First();
                     xx.amount += temp.amount;
                 }
                 else
                 {
                     allNumlist.Add(temp);
                 }
             }

                
            }
            else if (plateformType == "0")
            {
                //实体店
                List<SelectForDecimalTendency> syjIncomeList = BllSYJCommon.SelectEntityBySql<SelectForDecimalTendency>("t_pos_syd", syj_day_income_sql);
                allIncomelist.AddRange(syjIncomeList);

                List<SelectForTendency> syjNumList = BllSYJCommon.SelectEntityBySql<SelectForTendency>("t_pos_syd", syj_day_order_num_sql);
                allNumlist.AddRange(syjNumList);
            }
            else if (plateformType == "1")
            {
                //电商
                List<SelectForDecimalTendency> bbcIncomeList = BllBBCCommon.SelectEntityBySql<SelectForDecimalTendency>("orders", bbc_day_income_sql);
                allIncomelist.AddRange(bbcIncomeList);

                List<SelectForTendency> bbcNumList = BllBBCCommon.SelectEntityBySql<SelectForTendency>("orders", bbc_day_order_num_sql);
                allNumlist.AddRange(bbcNumList);
            }

            foreach (SelectForDecimalTendency tendency in allIncomelist)
            {
               int totalOrderNum= allNumlist.Where(x => x.timeStr == tendency.timeStr).First().amount;
               decimal costPerOrder = 0;
               if (totalOrderNum > 0)
               {
                  costPerOrder= tendency.amount / totalOrderNum;
               }
               SelectForDecimalTendency model = new SelectForDecimalTendency();
               model.timeStr = tendency.timeStr;
               model.amount = costPerOrder;

               list.Add(model);
            }
            decimal costPerOrderForSelectTime = 0;
            if (allNumlist.Sum(x => x.amount) > 0)
            {
                costPerOrderForSelectTime = Math.Round(allIncomelist.Sum(x => x.amount) / allNumlist.Sum(x => x.amount).ConToDecimal(),2);
            }

            Session["costPerOrder"] = costPerOrderForSelectTime;

            return list;
        }

        #endregion

        #region 销售额

        /// <summary>
        /// 销售额趋势（按天）
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TendencyDecimalModel> DaySaleTendency(string beginTime, string endTime, string plateformType)
        {
            string syj_day_income_sql = "select convert(char(10), ct, 120) as timeStr, isnull(sum(dcpayment-dcchange-isnull(mlje,0)),0) as amount from t_pos_syd where ct>='" + beginTime + "' and ct<='" + endTime + "' group by convert(char(10), ct, 120)";
            string bbc_day_income_sql = "select convert(char(10),payTime, 120) as timeStr, isnull(sum(realTotal),0) as amount from orders where paystatusId=2 and payTime<='" + beginTime + "' and payTime>='" + endTime + "' and plateform=3 group by convert(char(10),payTime, 120)";

            List<SelectForDecimalTendency> list = GetSaleTendency(plateformType, syj_day_income_sql, bbc_day_income_sql);

            List<TendencyDecimalModel> result = DayDecimalTendency(beginTime, endTime, list);

            return result;
        }

        /// <summary>
        /// 销售额趋势（按小时）
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TendencyDecimalModel> TimeSaleTendency(string beginTime, string endTime, string plateformType)
        {
            string syj_time_income_sql = "select convert(char(2), ct, 108) as timeStr, isnull(sum(dcpayment-dcchange-isnull(mlje,0)),0) as amount from t_pos_syd where ct>='" + beginTime + "' and ct<='" + endTime + "' group by convert(char(2), ct, 108)";
            string bbc_time_income_sql = "select convert(char(2),payTime, 108) as timeStr, isnull(sum(realTotal),0) as amount from orders where paystatusId=2 and payTime<='" + beginTime + "' and payTime>='" + endTime + "' and plateform=3 group by convert(char(2),payTime, 108)";

            List<SelectForDecimalTendency> list = GetSaleTendency(plateformType, syj_time_income_sql, bbc_time_income_sql);

            List<TendencyDecimalModel> result = TimeDecimalTendency(list);

            return result;
        }

        private List<SelectForDecimalTendency> GetSaleTendency(string plateformType, string syj_day_income_sql, string bbc_day_income_sql)
        {
            List<SelectForDecimalTendency> allIncomelist = new List<SelectForDecimalTendency>();

            if (plateformType == "-1")
            {
                //全部
                List<SelectForDecimalTendency> syjIncomeList = BllSYJCommon.SelectEntityBySql<SelectForDecimalTendency>("t_pos_syd", syj_day_income_sql);
                List<SelectForDecimalTendency> bbcIncomeList = BllBBCCommon.SelectEntityBySql<SelectForDecimalTendency>("orders", bbc_day_income_sql);

                allIncomelist.AddRange(syjIncomeList);
                foreach (SelectForDecimalTendency temp in bbcIncomeList)
                {
                    bool flag = allIncomelist.Select(x => x.timeStr).Contains(temp.timeStr);
                    if (flag)
                    {
                        SelectForDecimalTendency xx = allIncomelist.Where(x => x.timeStr == temp.timeStr).First();
                        xx.amount += temp.amount;
                    }
                    else
                    {
                        allIncomelist.Add(temp);
                    }
                }

            }
            else if (plateformType == "0")
            {
                //实体店
                List<SelectForDecimalTendency> syjIncomeList = BllSYJCommon.SelectEntityBySql<SelectForDecimalTendency>("t_pos_syd", syj_day_income_sql);
                allIncomelist.AddRange(syjIncomeList);
            }
            else if (plateformType == "1")
            {
                //电商
                List<SelectForDecimalTendency> bbcIncomeList = BllBBCCommon.SelectEntityBySql<SelectForDecimalTendency>("orders", bbc_day_income_sql);
                allIncomelist.AddRange(bbcIncomeList);
            }

            return allIncomelist;
        }

        #endregion

        #endregion

        #region 共同类/方法

        #region 共同方法
        /// <summary>
        /// 获取图表数据
        /// </summary>
        /// <param name="type">1：浏览量 2：访客数 3：订单量 4：客单价 5：销售额</param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="i">1：多日数据 2：单日数据</param>
        /// <param name="nameList"></param>
        /// <param name="productName"></param>
        /// <returns></returns>
        private List<List<decimal>> GetChartData(int type,string beginTime,string endTime,int i,ref List<string> nameList,string productName=null,string plateformType="-1")
        {
            List<List<decimal>> dataList=new List<List<decimal>>();
            if (i == 1)
            {
                List<TendencyModel> list = new List<TendencyModel>();
                List<TendencyDecimalModel> listDecimal = new List<TendencyDecimalModel>();
                if (type == 1)
                {
                    //浏览量
                    list = DayScanTendency(beginTime, endTime, productName);
                }
                else if (type == 2)
                {
                    //访客数
                    list = DayLoginTendency(beginTime, endTime);
                }
                else if (type == 3)
                {
                    //订单量
                    list = DayOrderTendency(beginTime, endTime,plateformType);
                }
                else if (type == 4)
                {
                    //客单价
                    listDecimal = DayCostTendency(beginTime, endTime, plateformType);
                }
                else if (type == 5)
                {
                    //销售额
                    listDecimal = DaySaleTendency(beginTime, endTime, plateformType);
                }

                List<decimal> amountList = new List<decimal>();
                List<decimal> totalList = new List<decimal>();
                List<decimal> rateList = new List<decimal>();
                int before = 0;
                decimal beforeDecimal = 0;
                foreach (TendencyModel model in list)
                {
                    nameList.Add(model.timeStr);
                    amountList.Add(model.amount);
                    totalList.Add(model.total);
                    decimal rate = 0;
                    if (before > 0)
                    {
                       rate= Math.Round((model.amount - before).ConToDecimal() / before.ConToDecimal(), 4) * 100;
                    }
                    rateList.Add(rate);
                    before = model.amount;
                }
                foreach (TendencyDecimalModel model in listDecimal)
                {
                    nameList.Add(model.timeStr);
                    amountList.Add(model.amount);
                    totalList.Add(model.total);
                    decimal rate = 0;
                    if (beforeDecimal > 0)
                    {
                        rate = Math.Round((model.amount - beforeDecimal).ConToDecimal() / beforeDecimal.ConToDecimal(), 4) * 100;
                    }
                    rateList.Add(rate);
                    beforeDecimal = model.amount;
                }

                dataList.Add(amountList);
                dataList.Add(totalList);
                dataList.Add(rateList);
            }
            else
            {
                string beginTime_ = beginTime.ConToDateTime(DateTime.Now).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
                string endTime_ = endTime.ConToDateTime(DateTime.Now).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");

                List<TendencyModel> listNow = new List<TendencyModel>();
                List<TendencyModel> listBefore = new List<TendencyModel>();

                List<TendencyDecimalModel> listNowDecimal = new List<TendencyDecimalModel>();
                List<TendencyDecimalModel> listBeforeDecimal = new List<TendencyDecimalModel>();

                if (type == 1)
                {
                    //浏览量
                    listNow = TimeScanTendency(beginTime, endTime,productName);
                    listBefore = TimeScanTendency(beginTime_, endTime_,productName);
                }
                else if (type == 2)
                {
                    //访客数
                    listNow = TimeLoginTendency(beginTime, endTime);
                    listBefore = TimeLoginTendency(beginTime_, endTime_);
                }
                else if (type == 3)
                {
                    //订单量
                    listNow = TimeOrderTendency(beginTime, endTime, plateformType);
                    listBefore = TimeOrderTendency(beginTime_, endTime_, plateformType);
                }
                else if (type == 4)
                {
                    //客单价
                    listNowDecimal = TimeCostTendency(beginTime, endTime, plateformType);
                    listBeforeDecimal = TimeCostTendency(beginTime_, endTime_, plateformType);
                }
                else if (type == 5)
                {
                    //销售额
                    listNowDecimal = TimeSaleTendency(beginTime, endTime, plateformType);
                    listBeforeDecimal = TimeSaleTendency(beginTime_, endTime_, plateformType);
                }

                List<decimal> totalNowList = new List<decimal>();
                List<decimal> totalBeforeList = new List<decimal>();
                List<decimal> totalNowAllList = new List<decimal>();
                List<decimal> totalBeforeAllList = new List<decimal>();

                for (int j = 0; j < listNow.Count; j++)
                {
                    nameList.Add(listNow[j].timeStr);
                    totalNowList.Add(listNow[j].amount);
                    totalBeforeList.Add(listBefore[j].amount);
                    totalNowAllList.Add(listNow[j].total);
                    totalBeforeAllList.Add(listBefore[j].total);
                }

                for (int j = 0; j < listNowDecimal.Count; j++)
                {
                    nameList.Add(listNowDecimal[j].timeStr);
                    totalNowList.Add(listNowDecimal[j].amount);
                    totalBeforeList.Add(listBeforeDecimal[j].amount);
                    totalNowAllList.Add(listNowDecimal[j].total);
                    totalBeforeAllList.Add(listBeforeDecimal[j].total);
                }

                dataList.Add(totalNowList);
                dataList.Add(totalBeforeList);
                dataList.Add(totalNowAllList);
                dataList.Add(totalBeforeAllList);
            }

            return dataList;
        }

        /// <summary>
        /// 多日趋势
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<TendencyModel> DayTendency(string beginTime,string endTime,List<SelectForTendency> list)
        {
            if (list == null)
            {
                list = new List<SelectForTendency>();
            }

            DateTime beginTime_ = beginTime.ConToDateTime(DateTime.Now);
            DateTime endTime_ = endTime.ConToDateTime(DateTime.Now);
            DateTime start = Convert.ToDateTime(beginTime_.ToShortDateString());
            DateTime end = Convert.ToDateTime(endTime_.ToShortDateString());
            TimeSpan sp = end.Subtract(start);
            int dayNum = sp.Days;

            for (int i = 0; i <= dayNum; i++)
            {
                string temp = beginTime_.AddDays(i).ToString("yyyy-MM-dd");

                bool flag = list.Select(x => x.timeStr).Contains(temp);
                if (!flag)
                {
                    SelectForTendency income = new SelectForTendency();
                    income.timeStr = temp;
                    income.amount = 0;

                    list.Add(income);
                }
            }

            list = list.OrderBy(x => x.timeStr).ToList();

            List<TendencyModel> result = new List<TendencyModel>();

            foreach (SelectForTendency tendency in list)
            {
                TendencyModel model = new TendencyModel();
                model.timeStr = tendency.timeStr;
                model.amount = tendency.amount;
                model.total = result.Sum(x => x.amount) + tendency.amount;

                result.Add(model);
            }

            return result;
        }

        /// <summary>
        /// 多日趋势
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<TendencyDecimalModel> DayDecimalTendency(string beginTime, string endTime, List<SelectForDecimalTendency> list)
        {
            if (list == null)
            {
                list = new List<SelectForDecimalTendency>();
            }

            DateTime beginTime_ = beginTime.ConToDateTime(DateTime.Now);
            DateTime endTime_ = endTime.ConToDateTime(DateTime.Now);
            DateTime start = Convert.ToDateTime(beginTime_.ToShortDateString());
            DateTime end = Convert.ToDateTime(endTime_.ToShortDateString());
            TimeSpan sp = end.Subtract(start);
            int dayNum = sp.Days;

            for (int i = 0; i <= dayNum; i++)
            {
                string temp = beginTime_.AddDays(i).ToString("yyyy-MM-dd");

                bool flag = list.Select(x => x.timeStr).Contains(temp);
                if (!flag)
                {
                    SelectForDecimalTendency income = new SelectForDecimalTendency();
                    income.timeStr = temp;
                    income.amount = 0;

                    list.Add(income);
                }
            }

            list = list.OrderBy(x => x.timeStr).ToList();

            List<TendencyDecimalModel> result = new List<TendencyDecimalModel>();

            foreach (SelectForDecimalTendency tendency in list)
            {
                TendencyDecimalModel model = new TendencyDecimalModel();
                model.timeStr = tendency.timeStr;
                model.amount = Math.Round(tendency.amount, 2);
                model.total = Math.Round(result.Sum(x => x.amount) + tendency.amount,2);

                result.Add(model);
            }

            return result;
        }

        /// <summary>
        /// 单日趋势
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<TendencyModel> TimeTendency(List<SelectForTendency> list)
        {
            if (list == null)
            {
                list = new List<SelectForTendency>();
            }

            #region 转换成24小时制度
            for (int i = 0; i < 24; i++)
            {
                string timeStr = "";
                if (i < 10)
                {
                    timeStr = "0" + i.ToString();
                }
                else
                {
                    timeStr = i.ToString();
                }

                bool flag = list.Select(x => x.timeStr).Contains(timeStr);
                if (!flag)
                {
                    SelectForTendency income = new SelectForTendency();
                    income.timeStr = timeStr;
                    income.amount = 0;
                    list.Add(income);
                }
            }
            #endregion

            //正向排序
            list = list.OrderBy(x => x.timeStr.ConToInt()).ToList();

            List<TendencyModel> result = new List<TendencyModel>();

            foreach (SelectForTendency tendency in list)
            {
                int temp = tendency.timeStr.ConToInt() + 1;
                string next = temp < 10 ? "0" + temp : temp.ToString();

                TendencyModel model = new TendencyModel();
                model.timeStr = tendency.timeStr + ":00" + "-" + next + ":00";
                model.amount = tendency.amount;
                model.total = result.Sum(x => x.amount) + tendency.amount;

                result.Add(model);
            }

            return result;
        }

        /// <summary>
        /// 单日趋势
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<TendencyDecimalModel> TimeDecimalTendency(List<SelectForDecimalTendency> list)
        {
            if (list == null)
            {
                list = new List<SelectForDecimalTendency>();
            }

            #region 转换成24小时制度
            for (int i = 0; i < 24; i++)
            {
                string timeStr = "";
                if (i < 10)
                {
                    timeStr = "0" + i.ToString();
                }
                else
                {
                    timeStr = i.ToString();
                }

                bool flag = list.Select(x => x.timeStr).Contains(timeStr);
                if (!flag)
                {
                    SelectForDecimalTendency income = new SelectForDecimalTendency();
                    income.timeStr = timeStr;
                    income.amount = 0;
                    list.Add(income);
                }
            }
            #endregion

            //正向排序
            list = list.OrderBy(x => x.timeStr.ConToInt()).ToList();

            List<TendencyDecimalModel> result = new List<TendencyDecimalModel>();

            foreach (SelectForDecimalTendency tendency in list)
            {
                int temp = tendency.timeStr.ConToInt() + 1;
                string next = temp < 10 ? "0" + temp : temp.ToString();

                TendencyDecimalModel model = new TendencyDecimalModel();
                model.timeStr = tendency.timeStr + ":00" + "-" + next + ":00";
                model.amount = Math.Round(tendency.amount, 2);
                model.total = Math.Round(result.Sum(x => x.amount) + tendency.amount, 2);

                result.Add(model);
            }

            return result;
        }
        #endregion

        #region 共同类
        private class SelectForTendency
        {
            public string timeStr { get; set; }

            public int amount { get; set; }
        }

        private class TendencyModel
        {
            public string timeStr { get; set; }

            public int amount { get; set; }

            public int total { get; set; }
        }

        private class SelectForDecimalTendency
        {
            public string timeStr { get; set; }

            public decimal amount { get; set; }
        }
        private class TendencyDecimalModel
        {
            public string timeStr { get; set; }

            public decimal amount { get; set; }

            public decimal total { get; set; }
        }
        #endregion

        #endregion
    }
}